const BaseService = require("./BaseService.js")
const moment = require('moment')

class StatisticsService extends BaseService {
  // 文章类型堆叠统计
  async t1(){
    // 公司名称
    let company = [
      {
        name: '泛生子',
        type: []
      },
      {
        name: '吉因加',
        type: []
      },
      {
        name: '求臻医学',
        type: []
      },
      {
        name: '燃石',
        type: []
      },
      {
        name: '世和',
        type: []
      },
      {
        name: '思路迪',
        type: []
      },
      {
        name: '先声诊断',
        type: []
      },
      {
        name: '臻和科技',
        type: []
      },
      {
        name: '至本',
        type: []
      }
    ]
    // 文章分类
    let article_type = [
      {
        type: 'DNA测序研究',
        count: 0
      },
      {
        type: 'II期限临床试验',
        count: 0
      },
      {
        type: 'RNA测序研究',
        count: 0
      },
      {
        type: '按例报道',
        count: 0
      },
      {
        type: '方法研究',
        count: 0
      },
      {
        type: '非肿瘤',
        count: 0
      },
      {
        type: '分子机制研究',
        count: 0
      },
      {
        type: '公共数据库分析',
        count: 0
      },
      {
        type: '回顾性研究',
        count: 0
      },
      {
        type: '技术研发',
        count: 0
      },
      {
        type: '技术验证',
        count: 0
      },
      {
        type: '家系研究',
        count: 0
      },
      {
        type: '临床队列研究',
        count: 0
      },
      {
        type: '临床试验',
        count: 0
      },
      {
        type: '软件工具算法',
        count: 0
      },
      {
        type: '真实世界研究',
        count: 0
      },
      {
        type: '综述',
        count: 0
      }
    ];
    // 组合统计
    for (let i = 0; i <= company.length-1; i++) {
      let result = await this.Db('article').where('company','=',company[i].name).select();
      let type = article_type.map(itm=>{
        let count = result.filter(item=>{
          if(item.article_type.indexOf(itm.type) > -1){
            return true
          }else{
            return false
          }
        })
        return {
          type: itm.type,
          count: count.length
        }
      })
      company[i].type = type
    }
    return company
  }

  // 公司得分堆叠统计
  async t2(){
    // 公司名称
    let company = [
      {
        name: '泛生子',
        type: []
      },
      {
        name: '吉因加',
        type: []
      },
      {
        name: '求臻医学',
        type: []
      },
      {
        name: '燃石',
        type: []
      },
      {
        name: '世和',
        type: []
      },
      {
        name: '思路迪',
        type: []
      },
      {
        name: '先声诊断',
        type: []
      },
      {
        name: '臻和科技',
        type: []
      },
      {
        name: '至本',
        type: []
      }
    ]
    // 组合统计
    for (let i = 0; i <= company.length-1; i++) {
      let result = await this.Db('article').where('company','=',company[i].name).select();
      // >20分
      let count1 = result.filter(item=>{
        if(item.factor > 20){
          return true
        }else{
          return false
        }
      })
      // 1-3分
      let count2 = result.filter(item=>{
        if(item.factor >= 1 && item.factor <= 3){
          return true
        }else{
          return false
        }
      })
      // 10-20分
      let count3 = result.filter(item=>{
        if(item.factor >= 10 && item.factor <= 20){
          return true
        }else{
          return false
        }
      })
      // 3-5分
      let count4 = result.filter(item=>{
        if(item.factor >= 3 && item.factor <= 5){
          return true
        }else{
          return false
        }
      })
      // 5-10分
      let count5 = result.filter(item=>{
        if(item.factor >= 5 && item.factor <= 10){
          return true
        }else{
          return false
        }
      })
      company[i].type = [
        {
          name: '>20分',
          count: count1.length
        },
        {
          name: '1-3分',
          count: count2.length
        },
        {
          name: '10-20分',
          count: count3.length
        },
        {
          name: '3-5分',
          count: count4.length
        },
        {
          name: '5-10分',
          count: count5.length
        }
      ];
    }
    return company
  }
}

module.exports = StatisticsService;